package com.shinhye.mobilelottery.ui;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.shinhye.mobilelottery.ConstantValue;
import com.shinhye.mobilelottery.GlobalParams;
import com.shinhye.mobilelottery.R;
import com.shinhye.mobilelottery.bean.User;
import com.shinhye.mobilelottery.engine.UserEngine;
import com.shinhye.mobilelottery.net.protocol.Message;
import com.shinhye.mobilelottery.net.protocol.Oelement;
import com.shinhye.mobilelottery.net.protocol.element.BalanceElement;
import com.shinhye.mobilelottery.ui.manager.MiddleManager;
import com.shinhye.mobilelottery.ui.manager.TitleManager;
import com.shinhye.mobilelottery.utils.BeanFactory;
import com.shinhye.mobilelottery.utils.PromptManager;

public class UserLoginUI extends BaseUI implements OnClickListener{
	private EditText username;
	private ImageView clear;// 清空用户名
	private EditText password;
	private Button login;
	
	public UserLoginUI(Context context) {
		super(context);
	}

	@Override
	protected void onCreate() {
		View showView = View.inflate(getContext(), R.layout.il_user_login, null);
		
		username = (EditText) showView.findViewById(R.id.ii_user_login_username);
		clear = (ImageView) showView.findViewById(R.id.ii_clear);
		password = (EditText) showView.findViewById(R.id.ii_user_login_password);
		login = (Button) showView.findViewById(R.id.ii_user_login);
		
		setContentView(showView);
	}

	@Override
	protected void setListener() {
		clear.setOnClickListener(this);
		login.setOnClickListener(this);
		
		username.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {}
			@Override
			public void afterTextChanged(Editable s) {
				if(username.getText().toString().length() > 0) {
					clear.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	@Override
	public int getId() {
		return ConstantValue.VIEW_USERLOGIN;
	}

	@Override
	public void onResume() {
		changeTitle();
	}

	private void changeTitle() {
		String title = "用户登录";
		TitleManager.getInstance().changeTitle(title);		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.ii_clear:
			username.setText("");
			clear.setVisibility(View.INVISIBLE);
			break;
		case R.id.ii_user_login:
			if(checkUserInfo()) {
				User user = new User();
				user.setUsername(username.getText().toString().trim());
				user.setPassword(password.getText().toString().trim());
				new MyAsyncTask<User>() {
					@Override
					protected void onPreExecute() {
						PromptManager.showProgressDialog(getContext());
					}
					@Override
					protected Message doInBackground(User... params) {
						UserEngine engine = BeanFactory.getImpl(UserEngine.class);
						Message login = engine.login(params[0]);
						if(login != null) {
							Oelement oelement = login.getBody().getOelement();
							if(ConstantValue.SUCCESS.equals(oelement.getErrorcode())) {
								GlobalParams.isLogin = true;
								GlobalParams.username = params[0].getUsername();
								//获取用户余额信息
								Message balance = engine.balance(params[0]);
								if(balance != null) {
									oelement = balance.getBody().getOelement();
									if(ConstantValue.SUCCESS.equals(oelement.getErrorcode())) {
										BalanceElement element = (BalanceElement) balance.getBody().getElements().get(0);
										GlobalParams.money = Float.parseFloat(element.getInvestvalues());
										return balance;
									} else {
										PromptManager.showToast(getContext(), "获取余额信息失败，请重试！");
									}
								} else {
									PromptManager.showToast(getContext(), "网络出现错误！");
								}
							} else {
								PromptManager.showToast(getContext(), "获取登录信息失败，请重试！");
							}
						} else {
							PromptManager.showToast(getContext(), "网络出现错误！");
						}
						return null;
					}
					
					@Override
					protected void onPostExecute(Message result) {
						PromptManager.closeProgressDialog();
						if(result != null) {
							MiddleManager.getInstance().goback();
							PromptManager.showToast(getContext(), "登录成功！");
						}
					}
				}.executeHttp(user);
			}
			break;
		}
	}
	
	private boolean checkUserInfo() {
		if(username.getText().toString().trim().length() != 11) {
			username.setError("用户名输入有误");
			return false;
		}
		return true;
	}
}
